/***************************************************************************
				DcaPreprocessor.cpp
                                -------------------
Copyright (c) 2009-2010 
Author: Kishor D. Bhalerao (kishor8dm@gmail.com)
Contributors: 

This file is associated with the Ph.D. dissertation,"ON METHODS FOR EFFICIENT
AND ACCURATE DESIGN AND SIMULATION OF MULTIBODY SYSTEMS", Bhalerao K.D,
Rensselaer Polytechnic Institute, 2010.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1.) Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2.) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3.) The name of the authors, their employers, or sponsoring organizations may
not be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHORS, THEIR EMPLOYERS, OR SPONSORING ORGANIZATIONS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/

#include "DcaPreprocessor.h"
#include "MatOps.h"

// This function is invoked just once before the start of the simulation

void DcaPreprocessor::Init(){
	int nBodies = matter_.getNumBodies();
	MatOp op;
	
	for(int i = 0;i<nBodies-1;i++){
		
		// Set Id of each body equal to that of the SimTK Id
		// No need to store data for ground body.
		Body[i].Id = i+1;
		
		// Get current body
		const MobilizedBody& body = matter_.getMobilizedBody(MobilizedBodyIndex(i+1));

		Mat33 M_C_B = ~(body.getOutboardFrame(state_).R());

		// Vector from body origin to center of mass. Expressed in Mobilizer basis (M)
		Body[i].BtoCM = M_C_B*body.getBodyMassCenterStation(state_);

		// These vectors are expressed in Mobilizer basis (M). 
		Vec3 cm2H1,cm2H2;
		
		// Get Handle Vectors
		if (i<nBodies-2){
			const MobilizedBody& child = matter_.getMobilizedBody(MobilizedBodyIndex(Body[i].Id+1));
			Body[i].cm2H2 = M_C_B*child.getInboardFrame(state_).T()-Body[i].BtoCM;
		}
		else{
			Body[i].cm2H2 = 0;
		}
		Body[i].cm2H1 = M_C_B*body.getOutboardFrame(state_).T()-Body[i].BtoCM;

		//Form handle cross matrices		
		Mat33 cmtoH1cross(0),cmtoH2cross(0);
		op.VectorCrossMatrix(&Body[i].cm2H1,&cmtoH1cross);
		op.VectorCrossMatrix(&Body[i].cm2H2,&cmtoH2cross);

		// Extract Mass Properties
		Real bodyMass = body.getBodyMass(state_);

		//Inertia in Mobilizer Basis
		Body[i].Iner = M_C_B*body.getBodyMassProperties(state_).calcCentralInertia().toMat33()*(~M_C_B);
		/*if (i == 0)
		Body[i].Iner(0,0) = 1; */
		
		Mat33 iMass(0);
		iMass(0,0)=1/bodyMass;
		iMass(1,1)=1/bodyMass;
		iMass(2,2)=1/bodyMass;

		Matrix InertiaMatrix = op.toMatrix(Body[i].Iner);
		SimTK::FactorLU lu(InertiaMatrix);
		Matrix iInertiaMatrix;
		lu.inverse(iInertiaMatrix);
		std::cout << i+1 << endl ;
		cout << body.getBodyMassProperties(state_).calcCentralInertia() << endl;
		


		Mat33 Z3(0), U3(1.0),TransMat;
		TransMat = (~cmtoH1cross);	
		Body[i].iIner = op.toMat33(iInertiaMatrix);

		//Shift Matrices To form equations about the handles (The shift Matrix defined here is 
		// the transpose of what is usually used (Kurt's Thesis))
		Body[i].SfH1 = op.toMat66(U3,Z3,TransMat,U3);
		TransMat = (~cmtoH2cross);
		Body[i].SfH2 = op.toMat66(U3,Z3,TransMat,U3);
		
		// The constant matrices formed in Mobilizer Basis (M).
		//Note I starts from 0. Body[0].psi11 is associated with the first body of the system Not the Ground
		// I need to modify this code by using Id instead of i later.
		Mat33 tmpMat33 = Body[i].iIner*cmtoH1cross;
		Body[i].psi11 = Body[i].SfH1*op.toMat66(Body[i].iIner,tmpMat33,Z3,iMass);
		Body[i].psi21 = Body[i].SfH2*op.toMat66(Body[i].iIner,tmpMat33,Z3,iMass);
		//Body[i].psi11 = Body[i].SfH1*op.toMat66(Body[i].iIner,Body[i].iIner*cmtoH1cross,Z3,iMass);
		//Body[i].psi21 = Body[i].SfH2*op.toMat66(Body[i].iIner,Body[i].iIner*cmtoH1cross,Z3,iMass);

		tmpMat33 = Body[i].iIner*cmtoH2cross;
		Body[i].psi12 =  Body[i].SfH1*op.toMat66(Body[i].iIner,tmpMat33,Z3,iMass);
		Body[i].psi22 =  Body[i].SfH2*op.toMat66(Body[i].iIner,tmpMat33,Z3,iMass);
		//Body[i].psi12 =  Body[i].SfH1*op.toMat66(Body[i].iIner,Body[i].iIner*cmtoH2cross,Z3,iMass);
		//Body[i].psi22 =  Body[i].SfH2*op.toMat66(Body[i].iIner,Body[i].iIner*cmtoH2cross,Z3,iMass);
	}
	
}
